package study.transaction.seata.support.mybatis;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.MybatisXMLLanguageDriver;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.type.JdbcType;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import javax.sql.DataSource;

@MapperScan(basePackages = "study.transaction.seata.mapper.user",
        sqlSessionFactoryRef = "user_sqlSessionFactory")
@MapperScan(basePackages = "study.transaction.seata.mapper.account",
        sqlSessionFactoryRef = "account_sqlSessionFactory")
@Slf4j
@Configuration
public class MybatisConfig {

    @Bean("user_sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("user_ds") DataSource ds) throws Exception {
        MybatisSqlSessionFactoryBean factoryBean = new MybatisSqlSessionFactoryBean();
        //Step-1: mybatis配置
        factoryBean.setDataSource(ds);
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        factoryBean.setMapperLocations(resolver.getResources("classpath:study/transaction/seata/mapper/user/**.xml"));
        factoryBean.setTypeAliasesPackage("study.transaction.seata.mapper.user");
        /*factoryBean.setPlugins(new Interceptor[]{
                new PaginationInterceptor(),
                new PerformanceInterceptor().setFormat(true),
        });*/
        //Step-2: mybatis plus配置
        MybatisConfiguration config = getMybatisConfiguration();
        factoryBean.setConfiguration(config);
        //Step-3: mybatis plus全局配置
        GlobalConfig globalConfig = getGlobalConfig();
        factoryBean.setGlobalConfig(globalConfig);

        return factoryBean.getObject();
    }

    @Bean("account_sqlSessionFactory")
    public SqlSessionFactory account_sqlSessionFactory(@Qualifier("account_ds") DataSource ds) throws Exception {
        MybatisSqlSessionFactoryBean factoryBean = new MybatisSqlSessionFactoryBean();
        //
        factoryBean.setDataSource(ds);
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        factoryBean.setMapperLocations(resolver.getResources("study/transaction/seata/mapper/account/*.xml"));
        factoryBean.setTypeAliasesPackage("study.transaction.seata.mapper.account");
        //
        MybatisConfiguration config = getMybatisConfiguration();
        factoryBean.setConfiguration(config);
        //
        GlobalConfig globalConfig = getGlobalConfig();
        factoryBean.setGlobalConfig(globalConfig);

        return factoryBean.getObject();
    }

    private MybatisConfiguration getMybatisConfiguration() {
        MybatisConfiguration config = new MybatisConfiguration();
        config.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
        config.setJdbcTypeForNull(JdbcType.NULL);
        config.setLogImpl(StdOutImpl.class);
        return config;
    }

    private GlobalConfig getGlobalConfig() {
        //Step-3: mybatis plus全局配置
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setBanner(false);
        //db config
        GlobalConfig.DbConfig dbConfig = new GlobalConfig.DbConfig();
        dbConfig.setIdType(IdType.AUTO);
        globalConfig.setDbConfig(dbConfig);
        return globalConfig;
    }
}
